#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#-------------------------------------------------------------------------------
#	file csv_lut.py
#	brief
#		Read a LUT from a csv fule.
#		Output a valid C file (DimLUT.c). This file has two functions:
#		One to get a LUT entry, the other to get the max value.
#
#	$Id: $
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#   Copyright and Disclaimer Notice
#
#	The MIT License (MIT)
#
#   Copyright (c) 2014 A.C. Verbeck
#
#	Permission is hereby granted, free of charge, to any person obtaining a copy
#	 of this software and associated documentation files (the "Software"), to deal
#	 in the Software without restriction, including without limitation the rights
#	 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#	 copies of the Software, and to permit persons to whom the Software is
#	 furnished to do so, subject to the following conditions:
#
#	The above copyright notice and this permission notice shall be included in
#	 all copies or substantial portions of the Software.
#
#	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#	 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#	 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#	 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#	 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#	 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#	 THE SOFTWARE.
#-------------------------------------------------------------------------------

__author__ = '$Author: $'
__version__ = '$Rev: $'
__date__ = '$Date: $'
__license__ = 'MIT'

import sys
import csv
import c_hdr
import argparse


#-------------------------------------------------------------------------------
#   gen_cfile
#	- Reads CSV input file
#	- Outputs the requested entries as a LUT
#
#	notes:
#	1)	To change the field width, adjust the fw variable.
#		Default is 5
#
#	2) 	To change the number of items on each line, adjust the ic variable.
#		Default is 16
#-------------------------------------------------------------------------------
def gen_cfile(opt):
	line_ct = 0																	# line count
	fw = 6																		# field width variable
	ic = 8																		# item count variable

	f_out = open(opt.output, 'w')												# open the output file
	f_out.write(c_hdr.dumpHeader())												# write header
	f_out.write('\t');															# write initial line tab

	with open(opt.input, 'rb') as f:											# open the csv file...
	    reader = csv.DictReader(f)												# ... open csv reader
	    for row in reader:														# go through the lines of the csv file
			o_str = '{}'.format(row[opt.table])									# build output string
			f_out.write(o_str.rjust(fw))										# write the value
			line_ct += 1														# bump the line count
			if line_ct == 256:													# end of the table?
				break															# exit
			f_out.write(',')													# write the comma
			if line_ct % ic == 0:												# every 16 numbers...
				f_out.write('\n\t');											# ...write a cr then a tab

	f_out.write(c_hdr.dumpFooter())												# write footer
	f_out.close()																# close the output file


#-------------------------------------------------------------------------------
#   parse_args:
#   lut arguments:
#   -- input: input file for the test run
#   -- output: output file: output from the test run
#   -- log: log file: technical output from the run
#-------------------------------------------------------------------------------
def parse_args():
	desc = 'CSV DimLUT C file generation utility'
	ap = argparse.ArgumentParser(description=desc)								# here's the script description...

	ap.add_argument("-i", "--input", default="lut_data.txt",					# argument: input file
					help="Input filename (default: %(default)s)")
	ap.add_argument("-t", "--table", default="std",								# argument: table value
					help="Input scale value (default: %(default)s)")
	ap.add_argument("-o", "--output", default="lut.c",							# argument: output file
					help="Output filename (default: %(default)s)")

	if len(sys.argv) == 1:														# if no args in cmd line
		ap.print_help()															# print the help
		sys.exit(1)																# exit
	return ap.parse_args()														# return the parsed object


#-------------------------------------------------------------------------------
#   main:
#   - Parse the arguments
#   - Run the c file generator
#-------------------------------------------------------------------------------
def main():
	options = parse_args()														# parse arguments
	gen_cfile(options)															# generate c file
	return 0																	# return A-OK...


#-------------------------------------------------------------------------------
#   Main processing
#-------------------------------------------------------------------------------
if __name__ == "__main__":														# if we are called as a script..
	sys.exit(main())															# call main

#
#   End: csv_lut.py
#
